package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itcat.api.mysql.PmsSkuStockApi;
import com.itcat.mapper.PmsSkuStockMapper;
import com.itcat.mysql.pojo.PmsProductLadder;
import com.itcat.mysql.pojo.PmsSkuStock;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-04 0:37
 */
@DubboService
public class PmsSkuStockApiImpl implements PmsSkuStockApi {

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;

    /**
     * 保存商品的sku库存信息
     * @param skuStockList
     */
    @Override
    public void saveList(List<PmsSkuStock> skuStockList) {
        pmsSkuStockMapper.batchInsert(skuStockList);
    }

    /**
     * 根据商品ID及sku编码模糊搜索sku库存
     * @param pid
     * @param keyword
     * @return
     */
    @Override
    public List<PmsSkuStock> getSkuByPid(Integer pid, String keyword) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId, pid);
        queryWrapper.like(keyword != null, PmsSkuStock::getSkuCode, keyword);
        List<PmsSkuStock> list = pmsSkuStockMapper.selectList(queryWrapper);
        return list;
    }


    /**
     * 批量更新sku库存信息
     * @param skuStockList
     */
    @Override
    public void updateSku(List<PmsSkuStock> skuStockList) {
        int i = pmsSkuStockMapper.batchUpdate(skuStockList);
    }

    /**
     * 获取商品的sku库存信息
     * @param productId
     * @return
     */
    @Override
    public List<PmsSkuStock> findByProductId(Long productId) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId, productId);
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockMapper.selectList(queryWrapper);
        return pmsSkuStockList;
    }

    /**
     * 更新商品的sku库存信息
     * @param skuStockList
     */
    @Override
    public void updateList(List<PmsSkuStock> skuStockList) {
        pmsSkuStockMapper.batchUpdateAll(skuStockList);
    }

    /**
     * 删除响应商品id的数据
     * @param productId
     */
    @Override
    public void deleteByProductId(Long productId) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(productId != null, PmsSkuStock::getProductId, productId);
        pmsSkuStockMapper.delete(queryWrapper);
    }
}
